<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_order\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\qbt_order\service\OrderService;
use think\Validate;
use think\Db;
use think\Config;
use think\Request;

class AdminIndexController extends PluginAdminBaseController
{
    public function _initialize()
    {
        $this->data = input();
        $this->config = Config::get('database');
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    public function index()
    {
        $status = $this->request->post('status');
        $order_sn = $this->request->post('order_sn','','intval');
        $consignee = $this->request->post('consignee');
        $where = '';
        if ($status != null) {
            switch ($status) {
                case 0:
                    $where['pay_status'] = 0;
                    break;
                case 1:
                    $where['shipping_status'] = 0;
                    break;            
                case 2:
                    $where['order_status'] = 1;
                    $where['pay_status'] = 2;
                    $where['shipping_status'] = 2;
                    break;                
                default:
                    # code...
                    break;
            }
        }
        
        if(!empty($order_sn)){
            $where['order_sn'] = $order_sn;
        }

        if(!empty($consignee)){
            $where['consignee'] = $consignee;
        }
        
        $p_list = OrderService::orderList($where);

        $this->assign('p_list',$p_list);
        $this->assign('page',$p_list->render());
        $this->assign('order_sn',$order_sn);
        $this->assign('consignee',$consignee);
        return $this->fetch('/menu');
    }

    /**
     * @adminLog(
     *     'public/plugin/qbt_order/admin_index/delOrder|删除订单:{order_id}',
     * )
     */
    public function delOrder()
    {
        $order_id = $this->request->param('order_id','','intval');
        !$order_id && $this->error('请输入合法的order_id');
        $res = OrderService::delOrder($order_id);
        if($res['code'] == 0){
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }
    
    /**
     * @adminLog(
     *     'public/plugin/qbt_order/admin_index/pay|更改订单状态(付款):{order_id}',
     * )
     */
    public function pay()
    {
        $order_id = $this->request->param('order_id','','intval');
        !$order_id && $this->error('请输入合法的order_id');
        $res = OrderService::pay($order_id);
        if($res['code'] == 0){
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }

    /**
     * @adminLog(
     *     'public/plugin/qbt_order/admin_index/receive|更改订单状态(确认收货):{order_id}',
     * )
     */
    public function receive()
    {
        $order_id = $this->request->param('order_id','','intval');
        !$order_id && $this->error('请输入合法的order_id');
        $res = OrderService::pay($order_id);
        if($res['code'] == 0){
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }
    /**
     * @adminLog(
     *     'public/plugin/qbt_order/admin_index/quxiao|更改订单状态(取消订单):{order_id}',
     * )
     */
    public function quxiao()
    {
        $order_id = $this->request->param('order_id','','intval');
        !$order_id && $this->error('请输入合法的order_id');
        $res = OrderService::pay($order_id);
        if($res['code'] == 0){
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }

    /**
     * @adminLog(
     *     'public/plugin/qbt_order/admin_index/confirmReceipt|更改订单状态(立即发货):{order_id}',
     * )
     */
    public function confirmReceipt()
    {
        $order_id = $this->request->param('order_id','','intval');
        $invoice_no = $this->request->param('invoice_no'); // 发货单号
        !$order_id && $this->error('请输入合法的order_id');
        !$invoice_no && $this->error('请输入发货单号');

        $res = OrderService::confirmReceipt($order_id,$invoice_no);
        if($res['code'] == 0){
            $this->success($res['msg']);
        } else {
            $this->error($res['msg']);
        }
    }     
    
    /**
     * 商品详情
     */    
    public function detail()
    {
        $order_id = $this->request->param('order_id','','intval');    
        $res = OrderService::detail($order_id);
        if($res['data']['order_goods']){
            foreach($res['data']['order_goods'] as $row)
            {
                $row['formated_subtotal']       = $this->price_format($row['goods_price'] * $row['goods_number']);
                $row['formated_goods_price']    = $this->price_format($row['goods_price']);
                $goods_list[] = $row;
            }
            
            $this->assign('goods_list',$goods_list);            
        }
        $this->assign('p_list',$res['data']['p_list'][0]);
        return $this->fetch('/detail');
    }

    /**
     * 更改收货方式
     */ 
    public function shipping()
    {
        if($this->request->isPost()){
            $shipping_name = $this->request->post('shipping_name');
            $order_id = $this->request->post('order_id');
            if($order_id){
                Db::name('order_info')->where(['order_id' => $order_id])->update(['shipping_name' => $shipping_name]);
            }
            $this->success('修改成功',cmf_plugin_url('QbtOrder://AdminIndex/detail?order_id='.$order_id));
            exit;
        }
        $order_id = $this->request->param('order_id');
        $order_shipping_name = DB::name('order_info')
                                ->where(['order_id' => $order_id])
                                ->value('shipping_name');
        $shipping = DB::query("SELECT shipping_name,shipping_desc FROM ".$this->config['prefix']."shipping");

        foreach ($shipping as &$value) {
            $value['is_check'] = '';
            if($value['shipping_name'] == $order_shipping_name){
                $value['is_check'] = 'checked';
            }
        }

        $this->assign('shipping',$shipping);
        $this->assign('order_id',$order_id);
        return $this->fetch('/shipping');
    }

    /**
     * 格式化商品价格
     *
     * @access  public
     * @param   float   $price  商品价格
     * @return  string
     */
    public function price_format($price, $change_price = true)
    {
        if($price==='')
        {
         $price=0;
        }

        $price = @number_format($price, 2, '.', '');
        return sprintf('￥%s', $price);
        //return sprintf('￥%s元', $price);
    }
}
